﻿#ifndef QTFFMPEGCORE_H
#define QTFFMPEGCORE_H

#include <QObject>

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavdevice/avdevice.h"
#include "libavfilter/avfilter.h"
#include "libavutil/avutil.h"
#include "libavutil/imgutils.h"
}

#if defined (Q_OS_WIN)
 #define AV_INPUT_FORMAT        "dshow"
#elif defined (Q_OS_LINUX)
 #define AV_INPUT_FORMAT        "video4linux2"
#elif defined (Q_OS_MAC)
 #define AV_INPUT_FORMAT        "avfoundation"
#endif

class QtFfmpegCore : public QObject
{
    Q_OBJECT
public:
    explicit QtFfmpegCore(QObject *parent = nullptr);

    QStringList getCameraNames(void);
    QStringList getAudioNames(void);
    QStringList getCameraOptions(const QString & name);

    virtual bool openDev(const QString & url) = 0;
    virtual void closeDev(void) = 0;

private:
    static void cameraOptionCallback(void * ptr, int level, const char * fmt, va_list vl);

    virtual void freeDev(void) = 0;

    QStringList getDeviceNames(enum AVMediaType mediaType);

signals:

protected:
    qreal rationalToDouble(AVRational * rational);

protected:
    AVFormatContext * m_formatCtx = nullptr;
    AVCodecContext * m_codecCtx = nullptr;

    static QStringList m_cameraOptions;
};

#endif // QTFFMPEGCORE_H
